package com.example.itshome.activity;

import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.itshome.R;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.RegisterRequest;
import com.example.itshome.network.SendVerifyCodeRequest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterPageActivity extends AppCompatActivity {

    private EditText nicknameEditText;
    private EditText emailEditText;
    private EditText passwordEditText;
    private EditText passwordConfirmEditText;
    private EditText verifyCodeEditText;
    private EditText usernameEditText;
    private TextView sendVerifyCodeTextView;
    private Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registe_page);
        initFind();
        initButton();
        initView();
    }

    void initFind(){
        nicknameEditText=findViewById(R.id.edt_register_page_nickname);
        emailEditText=findViewById(R.id.edt_register_page_email);
        passwordEditText=findViewById(R.id.edt_register_page_password);
        passwordConfirmEditText=findViewById(R.id.edt_register_page_password_confirm);
        verifyCodeEditText=findViewById(R.id.edt_register_page_verify_code);
        usernameEditText=findViewById(R.id.edt_register_page_username);
        sendVerifyCodeTextView=findViewById(R.id.txv_register_page_send_verify_code);
        registerButton=findViewById(R.id.btn_register_page_register);
    }

    void initButton(){
        sendVerifyCodeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AbstractNetworkRequest request=new SendVerifyCodeRequest(RegisterPageActivity.this)
                        .setEmail(emailEditText.getText().toString())
                        .setSendCodeTextView(sendVerifyCodeTextView);
                request.start();
            }
        });
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AbstractNetworkRequest request=new RegisterRequest(RegisterPageActivity.this)
                        .setEmail(emailEditText.getText().toString())
                        .setNickName(nicknameEditText.getText().toString())
                        .setPassword(passwordEditText.getText().toString())
                        .setPasswordConfirm(passwordConfirmEditText.getText().toString())
                        .setVerifyCode(verifyCodeEditText.getText().toString())
                        .setUsername(usernameEditText.getText().toString());
                request.start();
            }
        });
    }
    void initView(){
        usernameEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int focus= usernameEditText.getSelectionStart();
                String text= s.toString();
                Pattern pattern=Pattern.compile("[^0-9a-zA-Z_]");
                Matcher m=pattern.matcher(text);
                String res=m.replaceAll("");
                if(!res.equals(text)){
                    usernameEditText.setText(res);
                    usernameEditText.setSelection(Math.min(focus-1, res.length()));
                }

            }
        });
    }
}